package com.sinder.campus_prophet.service.jwgl.impl;

import cc.lyceum.api.thxy.jwgl.ThxyJwgl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinder.campus_prophet.core.global.ResponseMessage;
import com.sinder.campus_prophet.entity.jwgl.TbCultureProgram;
import com.sinder.campus_prophet.exception.JwglException;
import com.sinder.campus_prophet.mapper.jwgl.TbJwglInfoMapper;
import com.sinder.campus_prophet.service.jwgl.TbCultureProgramService;
import com.sinder.campus_prophet.mapper.jwgl.TbCultureProgramMapper;
import com.sinder.campus_prophet.service.jwgl.bean.dto.CultureProgramSynDto;
import com.sinder.campus_prophet.service.jwgl.bean.vo.CultureProgramVo;
import com.sinder.campus_prophet.service.jwgl.bean.vo.JwglInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

/**
* @author AS
* @description 针对表【tb_culture_program(培养方案)】的数据库操作Service实现
* @createDate 2023-03-15 23:13:21
*/
@Service
@Slf4j
public class TbCultureProgramServiceImpl extends ServiceImpl<TbCultureProgramMapper, TbCultureProgram>
    implements TbCultureProgramService{

    private final TbCultureProgramMapper cultureProgramMapper;

    private final TbJwglInfoMapper jwglInfoMapper;


    public TbCultureProgramServiceImpl(TbCultureProgramMapper cultureProgramMapper, TbJwglInfoMapper jwglInfoMapper) {
        this.cultureProgramMapper = cultureProgramMapper;
        this.jwglInfoMapper = jwglInfoMapper;
    }

    /**
     * 查询培养方案数据 绩点
     * @param openId
     * @return
     */
    @Override
    public ResponseMessage getCultureProgram(String openId) {
        CultureProgramVo cultureProgram = cultureProgramMapper.getCultureProgramByOpenId(openId);
        if (cultureProgram == null) {
            return ResponseMessage.createByErrorMessage("暂无数据，请移步同步数据");
        }
        return ResponseMessage.createBySuccess("培养方案数据", cultureProgram);
    }

    /**
     * 培养方案数据单独同步
     * @param cultureProgramSynDto
     * @return
     */
    @Async("taskExecutor")
    @Override
    public ResponseMessage synCultureProgramData(CultureProgramSynDto cultureProgramSynDto, ThxyJwgl jwgl) {
        log.info(Thread.currentThread().getThreadGroup() + "--------" + Thread.currentThread().getName());
        String openId = cultureProgramSynDto.getOpenId();
        // 数据同步开始时间
        long start = System.currentTimeMillis();
        try {
            log.info("----正在同步用户{}的培养方案新数据----", openId);
            Map<String, String> cultureProgram = jwgl.getStudyStatus();
            TbCultureProgram cultureProgramEntity = JSON.parseObject(JSON.toJSONString(cultureProgram), TbCultureProgram.class);
            // 执行更新，数据不存在则插入数据
            QueryWrapper<TbCultureProgram> cultureProgramQuery = new QueryWrapper<>();
            cultureProgramQuery.eq("wx_open_id", openId);
            cultureProgramEntity.setWxOpenId(openId);
            cultureProgramEntity.setSynTime(new Date());
            // 处理一下数据
            cultureProgramEntity.setTjmc1(StringUtils.substringBefore(cultureProgramEntity.getTjmc1(), "["));
            int cultureRow = cultureProgramMapper.update(cultureProgramEntity, cultureProgramQuery);
            if (cultureRow == 0) {
                cultureProgramMapper.insert(cultureProgramEntity);
            }
        } finally {
            if (jwgl != null) {
                jwgl.logout();
            }
        }
        // 数据同步结束时间
        long end = System.currentTimeMillis();
        log.info("培养方案已同步，共耗时：" + (end-start) / 1000 + "s" );
        return ResponseMessage.createBySuccessMessage("培养方案已同步，共耗时：" + (end-start) / 1000 + "s");
    }
}




